﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class DemonSlingScythe : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.friendly = true;      
            Projectile.hostile = false;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.penetrate = 5;
            Projectile.width = 22;
            Projectile.height = 22;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return Color.White;
        }
        public int bounceCount = 8;

        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            SoundEngine.PlaySound(SoundID.Item10, Projectile.position);
            if (Projectile.velocity.X != oldVelocity.X)
            {
                Projectile.position.X = Projectile.position.X + Projectile.velocity.X;
                Projectile.velocity.X = -oldVelocity.X;
            }
            if (Projectile.velocity.Y != oldVelocity.Y)
            {
                Projectile.position.Y = Projectile.position.Y + Projectile.velocity.Y;
                Projectile.velocity.Y = -oldVelocity.Y;
            }
            bounceCount--;
            Projectile.velocity *= 0.9f;

            if (bounceCount == 0)
            {
                Projectile.Kill();
            }
            return false; // return false because we are handling collision
        }

        public override void AI()
        {
            Lighting.AddLight(Projectile.position, 0.5f, 0.3f, 0.05f);
            Projectile.rotation += Projectile.direction * 0.8f;
            Projectile.velocity.Y = Projectile.velocity.Y + 0.2f;

            if (Projectile.velocity.X > 18f)
            {
                Projectile.velocity.X = 18f;
            }
            if (Projectile.velocity.X < -18f)
            {
                Projectile.velocity.X = -18f;
            }

            if (Projectile.velocity.Y > 18f)
            {
                Projectile.velocity.Y = 18f;
            }
            if (Projectile.velocity.Y < -18f)
            {
                Projectile.velocity.Y = -18f;
            }

            for (int i = 0; i < 2; i++)
            {
                Vector2 position78 = new Vector2(Projectile.position.X, Projectile.position.Y);
                int width74 = Projectile.width;
                int height74 = Projectile.height;
                int num258 = Dust.NewDust(position78, width74, height74, 27, 0f, 0f, 100, default, 1f);
                Main.dust[num258].noGravity = true;
            }
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;
        }

        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item10, Projectile.position);
            for (int i = 0; i < 30; i++)
            {
                int num615 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 27, Projectile.velocity.X, Projectile.velocity.Y, 100, default, 1.7f);
                Main.dust[num615].noGravity = true;
                Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 27, Projectile.velocity.X, Projectile.velocity.Y, 100, default, 1f);
            }
        }
    }
}
